<!-- 可以继续添加脚本助手 -->
<template>

    <el-collapse-item title="脚本" name="script">
        <span>脚本</span>
        <el-input ref="elInput" v-model="curComponent.hg3000.script" type="textarea" :rows="10" ></el-input>

        <div style="display: flex; flex-direction: column; gap: 10px; margin-top: 10px;">
            <el-button @click="getDrwFile">取图形文件</el-button>
            <el-button @click="getProjectPrefix">取项目标识</el-button>
            <el-button @click="getCmdTip">取脚本提示</el-button>
            <el-button @click="getCmd">取3000开出</el-button>
            <el-button @click="getCmdFromProject">取项目开出</el-button>
        </div>

        <DlgBackDrwFile  :dialog-visible="isShowDrwFile" :config-new-file="false" @res-file="handleResDrwFile" > </DlgBackDrwFile>
        <DlgSelectProjectPrefix  ref="refPrefix" :dialog-visible="isShowProjectPrefix" @res-select-prefix="handleResSelectPrefix" > </DlgSelectProjectPrefix>
        <DlgScriptTip  ref="refCmdTip" :dialog-visible="isShowCmdTip" @res-select="handleResSelectCmdTip" > </DlgScriptTip>
        <DlgSelectCmd  ref="refCmd" :dialog-visible="isShowCmd" @res-select="handleResSelectCmd" > </DlgSelectCmd>
        
        <DlgSelectCmdProject  ref="refCmdProject" :dialog-visible="isShowCmdProject" @res-select-cmd-project="handleResSelectCmdProject" > </DlgSelectCmdProject>

    </el-collapse-item>
    
</template>

<script>
import { mapState } from 'vuex'
import DlgBackDrwFile from '@/draw/drwfile/DlgBackDrwFile.vue';
import DlgSelectProjectPrefix from '@/custom-component/common/page/DlgSelectProjectPrefix.vue';
import DlgScriptTip from '@/custom-component/common/page/DlgScriptTip.vue';
import DlgSelectCmd from './DlgSelectCmd.vue';
import DlgSelectCmdProject from './DlgSelectCmdProject.vue';

export default {
    components: { DlgBackDrwFile, DlgSelectProjectPrefix, DlgScriptTip, DlgSelectCmd, DlgSelectCmdProject  },
    data() {
        return {
            cursorPosition: 10, // 记录当前光标位置
            insertString: "插入的字符串", // 要插入的字符串
            isShowDrwFile: false,
            isShowProjectPrefix: false,
            isShowCmdTip: false,
            isShowCmd: false,
            isShowCmdProject: false,
        }
    }, 
    computed: mapState([
        'curComponent',
    ]),
    methods: {
        insertText( insertString ) {
            const inputElement = this.$refs.elInput.$el; // 获取整个 el-input 组件的 DOM 元素
            const nativeInput = inputElement.querySelector('textarea');
            this.cursorPosition = nativeInput.selectionStart

            const value = this.curComponent.hg3000.script;

            // 在光标位置插入字符串
            const newValue =  value.slice(0, this.cursorPosition) + insertString +
                value.slice(this.cursorPosition);

            this.curComponent.hg3000.script = newValue;

            // 更新光标位置到插入字符串的末尾
            this.$nextTick(() => {
                nativeInput.setSelectionRange( this.cursorPosition + this.insertString.length,
                this.cursorPosition + this.insertString.length );
                this.$refs.elInput.focus();
            });
        },

        getDrwFile( ){
            this.isShowDrwFile = true
        },

        getProjectPrefix( ){
            this.$refs.refPrefix.init( )
            this.isShowProjectPrefix = true 
        },
        getCmdTip( ){
            this.$refs.refCmdTip.init( )
            this.isShowCmdTip = true
        },       
        getCmd( ){
            this.$refs.refCmd.init( )
            this.isShowCmd = true 
        },

        handleResDrwFile( code , res ){
            if( code === 0 ) {
                this.isShowDrwFile = false
                return 
            }
            let strFile
            if( res.savedir === "/" )
                strFile = res.savedir + res.filename
            else
                strFile = res.savedir + "/" + res.filename
            this.insertText( strFile )
            this.isShowDrwFile = false
        },

        handleResSelectPrefix( code, prefix ){
            if( code === 0 ) {
                this.isShowProjectPrefix = false
                return 
            } 
            this.insertText( prefix )   
            this.isShowProjectPrefix = false
        },

        handleResSelectCmdTip( code, cmdtip ){
            if( code === 0 ) {
                this.isShowCmdTip = false 
                return 
            } 
            this.insertText( cmdtip )   
            this.isShowCmdTip = false
        },
        handleResSelectCmd( code, cmd ){
            if( code === 0 ) {
                this.isShowCmd = false
                return 
            } 
            this.insertText( cmd )  
            this.isShowCmd = false
        },
        getCmdFromProject(   ){
            this.isShowCmdProject = true 
        },
        handleResSelectCmdProject( code, ctlid ){
            if( code === 1 ) {
                this.insertText( ctlid )  
            }
            this.isShowCmdProject = false

         },
    }
}
</script>

<style lang="scss" scoped>


</style>
